package com.jair.crawler.mijia;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class postTest2 {
	Map<String,String> map = new HashMap<String,String>();
	public static void main(String[] args) throws ClientProtocolException, IOException{
		String signValue = "mi7a3ce5887ac11293e6bb1ea919ada9b1er";
		String app_id = "100012";
		String app_key = "6aaac13ace7c8be19386cc6a0d90";
		String sign = org.apache.commons.lang3.StringUtils.upperCase(postTest2.encodeMd5Hex(app_id + postTest2.encodeBase64(app_id) + app_key));
		String url = "https://api.miren.mi.com/temporary/share/productImage";
		String url2 = "http://api.miren.t.n.mi.com/temporary/share/productImage";
		String url3 = "http://api.miren.t.n.mi.com/temporary/share/getsharedata";
		String dev_url = "http://miren.common.t.n.b2c.srv/share/share/getShareData";
		String test_url = "http://miren.common.test.b2c.srv/share/share/getShareData";
		Response response = Request.Post(test_url)
				//.viaProxy(new HttpHost("127.0.0.1", 8888))
				.bodyForm(Form.form()
			    		   .add("app_id", "100012")
			    		  // .add("app_key", "f65d38df3dbc4760a4f09b54d67d2fd8")
			    		   .add("sign", sign)//不计入算法里面，用于后面的对比
			    		   //.add("imei", "99000939134337")
			    		   //.add("login_type", "2")
			    		   //.add("auth", "9c50b3762e7248199a190e1906cc3a06")
			    		   //.add("lang", "zh")
			    		   .build())
				.execute();
		String str = response.returnContent().asString();
		str = StringEscapeUtils.unescapeJava(str);
		str = postTest2.format(str);
		System.out.println(str);
	}
	
	 public static String format(String jsonStr) {
	        int level = 0;
	        StringBuffer jsonForMatStr = new StringBuffer();
	        for(int i=0;i<jsonStr.length();i++){
	            char c = jsonStr.charAt(i);
	            if(level>0&&'\n'==jsonForMatStr.charAt(jsonForMatStr.length()-1)){
	                jsonForMatStr.append(getLevelStr(level));
	            }
	            switch (c) {
	            case '{':
	            case '[':
	                jsonForMatStr.append(c+"\n");
	                level++;
	                break;
	            case ',':
	                jsonForMatStr.append(c+"\n");
	                break;
	            case '}':
	            case ']':
	                jsonForMatStr.append("\n");
	                level--;
	                jsonForMatStr.append(getLevelStr(level));
	                jsonForMatStr.append(c);
	                break;
	            default:
	                jsonForMatStr.append(c);
	                break;
	            }
	        }
	         
	        return jsonForMatStr.toString();
	 
	    }
	     
	    private static String getLevelStr(int level){
	        StringBuffer levelStr = new StringBuffer();
	        for(int levelI = 0;levelI<level ; levelI++){
	            levelStr.append("\t");
	        }
	        return levelStr.toString();
	    }
	    
	    public static String encodeMd5Hex(String str) {
	        return DigestUtils.md5Hex(str);
	    }
	    
	    public static String encodeBase64(String str) {
	        return org.apache.commons.codec.binary.StringUtils.newStringUtf8(Base64.encodeBase64(str.getBytes()));
	    }
}
